const nav = document.querySelector('.tab_nav')
const pane = document.querySelectorAll('.tab_pane')
//tab切换
nav.addEventListener('click', e => {
  if (e.target.tagName === 'A') {
    pane.forEach(item => item.style.display = 'none')
    pane[e.target.dataset.nav].style.display = 'block'
  }
})

//点击表单提交
const form = document.querySelector('form[name="login"]')
const agree = document.querySelector('input[name="agree"]')
const account = document.querySelector('input[name="account"]')
const pw = document.querySelector('input[name="pw"]')
form.addEventListener('submit', e => {
  e.preventDefault()
  if (!agree.checked) {
    return alert('请勾选同意协议')
  }

  // 获取存储的用户数据
  const users = JSON.parse(localStorage.getItem('users')) || []
  if (users.length === 0) {
    return alert('没有注册用户，请先注册')
  }

  // 匹配用户
  const user = users.find(user =>
    (account.value.trim() === user.uname || account.value.trim() === user.phone) && pw.value.trim() === user.password
  )
  if (user) {
    //登录时间
    user.lastLogin = new Date().toLocaleString()
    localStorage.setItem('users', JSON.stringify(users))

    //保存当前登录用户信息
    localStorage.setItem('currentUser', JSON.stringify(user))

    alert('登录成功！')
    location.href = '../index.html'
  } else {
    alert('账户名或密码错误')
  }
})

